package com.example.socket.socket.fileio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

interface FileCopyRunner{
    void copiFile(File source, File target);
}

public class FileCopyDemo {

    private static void close(Closeable closeable){
        if(closeable!=null){
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
       FileCopyRunner noBuffStreanCopy= new FileCopyRunner() {
           @Override
           public void copiFile(File source, File target) {
               InputStream fin=null;
               OutputStream fout=null;
               try {
                   fin = new FileInputStream(source);
                   fout = new FileOutputStream(target);

                       int result = fin.read();
                       while (result != -1) {
                           fout.write(result);
                       }
                   } catch (Exception e) {
                       e.printStackTrace();
                   }finally {
                        close(fin);
                        close(fout);
                 }
               }
       };

       FileCopyRunner buffStreanCopy=new FileCopyRunner() {
           @Override
           public void copiFile(File source, File target) {
               InputStream fin=null;
               OutputStream fout=null;
               try {
                   fin=new BufferedInputStream(new FileInputStream(source));
                   fout=new BufferedOutputStream(new FileOutputStream(target));

                   byte[] buffer=new byte[1024];

                   int result;
                   while ((result=fin.read(buffer))!=-1){
                       fout.write(buffer,0,result);
                   }

               } catch (Exception e) {
                   e.printStackTrace();
               }finally {
                   close(fin);
                   close(fout);
               }
           }
       };

       FileCopyRunner nioBuffStreanCopy=new FileCopyRunner() {
           @Override
           public void copiFile(File source, File target) {
               FileChannel fin=null;
               FileChannel fout=null;
               try {
                   fin=new FileInputStream(source).getChannel();
                   fout=new FileOutputStream(target).getChannel();
                   //获取缓冲区字节对象
                   ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
                   while (fin.read(byteBuffer)!=-1){
                       byteBuffer.flip();
                       //判断是否读完
                       while (byteBuffer.hasRemaining()){
                           fout.write(byteBuffer);
                       }
                       byteBuffer.clear();
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }finally {
                   close(fin);
                   close(fout);
               }
           }
       };

       FileCopyRunner nioTransferCopy=new FileCopyRunner() {
           @Override
           public void copiFile(File source, File target) {
               FileChannel fin=null;
               FileChannel fout=null;

               try {
                   fin=new FileInputStream(source).getChannel();
                   fout=new FileOutputStream(target).getChannel();
                   long transferrd=0L;
                   while (transferrd!=fin.size()){
                       transferrd+= fin.transferTo(0, fin.size(), fout);
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }finally {
                   close(fin);
                   close(fout);
               }
           }
       };
    }
}
